package com.qiyuesuo.server.main;

import com.qiyuesuo.server.filter.AuthenFilter;
import com.qiyuesuo.server.servlet.DownloadServlet;
import com.qiyuesuo.server.servlet.FileInfoServlet;
import com.qiyuesuo.server.servlet.NewFileInfoServlet;
import com.qiyuesuo.server.servlet.UploadServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.DispatcherType;
import java.util.EnumSet;

/**
 * 启动器
 *
 * @author s77
 * @create 2020-4月-01 14:48
 */
public class Start {
    private final static Logger logger = LoggerFactory.getLogger(Start.class);

    public static void main(String[] args) {
        Server server = new Server(8081);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        //servlet mapping
        context.addServlet(DownloadServlet.class, "/download");
        context.addServlet(UploadServlet.class, "/upload");
        context.addServlet(FileInfoServlet.class, "/info");
        context.addServlet(NewFileInfoServlet.class, "/newinfo");
        //身份拦截器
        context.addFilter(AuthenFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));

        server.setHandler(context);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            logger.error("服务器启动失败", e);
        }
    }


}
